package com.sp.upload;

import java.io.*;

/**
 * 自定义的关于流的读写的静态方法类
 */
public class StreamUtils {

    /**
     * 功能: 将输入流转换成byte[]
     * @param inputStream
     * @return
     * @throws IOException
     */
    public static byte[] streamToByteArray(InputStream inputStream) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] b = new byte[1024];
        int len;
        while ((len = inputStream.read(b)) != -1){
            byteArrayOutputStream.write(b, 0, len);
        }
        byte[] array = byteArrayOutputStream.toByteArray();
        byteArrayOutputStream.close();
        return array;
    }

    /**
     * 功能: 将InputStream转换成String
     * @param inputStream
     * @return
     * @throws Exception
     */
    public static String streamToString(InputStream inputStream) throws Exception {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder stringBuilder = new StringBuilder();
        String line;
        while((line = bufferedReader.readLine()) != null) {
            stringBuilder.append(line + "\r\n");
        }
        return stringBuilder.toString();
    }


}
